import { getOrderSaveFaultTypeOptions } from '@/api/order'
import { ref } from 'vue'

/**
 * 获取修改工单/新建工单的故障类型(反馈故障)下拉选项
 */
export const useGetOrderSaveFaultTypeOptions = () => {
  const faultTypeOptions = ref<Array<OrderSaveAPIResponse.FaultTypeOption>>([])
  const getFaultTypeOptionsLoading = ref(false)
  const getFaultTypeOptionsKeyword = ref('')

  const getFaultTypeOptions = async () => {
    try {
      getFaultTypeOptionsLoading.value = true
      const res = await getOrderSaveFaultTypeOptions(getFaultTypeOptionsKeyword.value)
      getFaultTypeOptionsLoading.value = false
      if (res.data.code === 20000) {
        faultTypeOptions.value = res.data.data.row
        return ''
      }
      return res.data.message
    } catch (error) {
      return {
        code: 10000,
        message: '获取故障类型下拉选项失败，请稍后重试',
        data: {},
        success: false,
        hasOtherCode: false,
        otherCodePrefix: null,
      }
    }
  }

  return {
    faultTypeOptions,
    getFaultTypeOptionsKeyword,
    getFaultTypeOptionsLoading,
    getFaultTypeOptions,
  }
}
